package com.lhoverc.service.activity.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lhoverc.annotation.AddDateTime;
import com.lhoverc.annotation.AddUpdateTime;
import com.lhoverc.mapper.ActivityMapper;
import com.lhoverc.mapper.ActivityParticipationMapper;
import com.lhoverc.mapper.UserMapper;
import com.lhoverc.pojo.PageBean;
import com.lhoverc.pojo.VO.ActivityVO;
import com.lhoverc.pojo.VO.RecentActivityVO;
import com.lhoverc.pojo.entity.ActivityEntity;
import com.lhoverc.pojo.entity.ActivityParticipationEntity;
import com.lhoverc.service.activity.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Time;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: LiCheng
 * @CreateTime: 2025-04-20
 * @Description:
 * @Version: 1.0
 */
@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ActivityParticipationMapper activityParticipationMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 获取最近的5项未结束的活动
     *
     * @return
     */
    @Override
    public PageBean getRecentActivity() {
        //1.设置分页参数
        PageHelper.startPage(1, 5);
        //2.执行查询
        List<RecentActivityVO> recentAnnounceVOList = activityMapper.pageRecentActivity();
        Page<RecentActivityVO> p = (Page<RecentActivityVO>) recentAnnounceVOList;
        //3.封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    @Override
    public List<ActivityVO> getActivityList(String activityName, Integer status, LocalDateTime registrationDate, LocalDateTime activityDate) {

        List<ActivityVO> activityVOList = activityMapper.getActivityList(activityName, status, registrationDate, activityDate);

        return activityVOList;
    }

    @AddDateTime
    @Override
    public void addActivity(ActivityEntity activityEntity) {
        LocalDateTime now = LocalDateTime.now();
        if(now.isBefore(activityEntity.getRegistrationStart())){
            activityEntity.setStatus(1);
        }else if(now.isAfter(activityEntity.getRegistrationStart()) && now.isBefore(activityEntity.getRegistrationDeadline())){
            activityEntity.setStatus(2);
        } else if (now.isBefore(activityEntity.getActivityStart())) {
            activityEntity.setStatus(1);
        } else if (now.isAfter(activityEntity.getActivityStart()) && now.isBefore(activityEntity.getActivityDeadline())) {
            activityEntity.setStatus(3);
        } else if (now.isAfter(activityEntity.getActivityDeadline())) {
            activityEntity.setStatus(4);
        }
        activityMapper.addActivity(activityEntity);
    }


    @Override
    public void addRegisterActivity(Integer userId, Integer activityId) {
        ActivityParticipationEntity activityParticipationEntity = activityParticipationMapper.getByUserIdAndActivityId(userId, activityId);
        if (activityParticipationEntity != null) {
            if (activityParticipationEntity.getIsDelete() == 1)
                activityParticipationMapper.updateIsDeleteTo0(userId, activityId, LocalDateTime.now());
            return;
        }
        activityParticipationEntity = new ActivityParticipationEntity();
        activityParticipationEntity.setUserId(userId);
        activityParticipationEntity.setActivityId(activityId);
        activityParticipationEntity.setUsername(userMapper.getUsernameById(userId));
        activityParticipationEntity.setActivityName(activityMapper.getActivityNameById(activityId));
        activityParticipationEntity.setRegisterTime(LocalDateTime.now());
        activityParticipationMapper.insert(activityParticipationEntity);
    }

    @Override
    public List<Integer> getRegisterActivities(Integer userId) {
        List<Integer> registerIdList = activityParticipationMapper.getIdByUserId(userId);
        return registerIdList;
    }

    @Override
    public void deleteRegisterActivity(Integer userId, Integer activityId) {
        activityParticipationMapper.updateIsDeleteTo1(userId, activityId);
    }

    @Override
    public void deleteActivity(String id) {
        activityMapper.deleteActivity(id);
    }

    @AddUpdateTime
    @Override
    public void updateActivity(ActivityEntity activityEntity) {
        LocalDateTime now = LocalDateTime.now();
        if(now.isBefore(activityEntity.getRegistrationStart())){
            activityEntity.setStatus(1);
        }else if(now.isAfter(activityEntity.getRegistrationStart()) && now.isBefore(activityEntity.getRegistrationDeadline())){
            activityEntity.setStatus(2);
        } else if (now.isBefore(activityEntity.getActivityStart())) {
            activityEntity.setStatus(1);
        } else if (now.isAfter(activityEntity.getActivityStart()) && now.isBefore(activityEntity.getActivityDeadline())) {
            activityEntity.setStatus(3);
        } else if (now.isAfter(activityEntity.getActivityDeadline())) {
            activityEntity.setStatus(4);
        }
        activityMapper.updateActivity(activityEntity);
    }
}
